package com.seaengine.net
{
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.system.Security;

	public class TCPProtocol extends Socket
	{
		protected var _host:String = "";
		protected var _port:int	 = 0;
		
		//call back function
		public var onConnect:Function;			//function onConnect(void):void{}
		public var onError:Function;			//function onError(info:String):void{}
		public var onData:Function;				//function onData(info:*):void{}    function onData(client:Socket):void{}
		public var onClose:Function;			//function onClose(void):void{}
		
		protected static var m_serialNumber:uint = 0;
	
		public function TCPProtocol(host:String=null, port:int=0)
		{
			super(host, port);
			//this.endian = Endian.LITTLE_ENDIAN;
			if(host)
				_host = host;
				
			if(port)
				_port = port;
			configureListener();
//			Security.loadPolicyFile("http://" + GameConfig.m_ip + "/crossdomain.xml");
//			if(_host && _port && !connected)
//				Security.loadPolicyFile("xmlsocket://" + GameConfig.m_ip + ":843");
//				Security.loadPolicyFile("http://10.10.1.183/Client/crossdomain.xml");
//				this.connect(_host,_port);
		}
		
		private function configureListener():void
		{
			this.addEventListener(Event.CLOSE,closeHandler);
			this.addEventListener(Event.CONNECT,connectHandler);
			this.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			this.addEventListener(ProgressEvent.SOCKET_DATA,dataHandler);
			this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityError);
		}
		
		protected function send(buffer:JoyPortBuffer,len:uint=0,offset:uint=0):void
		{
			this.writeBytes(buffer,len,offset);
			this.flush();
		}
		
		override public function connect(host:String, port:int):void
		{
			if(this.connected)
			{
				return;
			}
			m_serialNumber = 0;
			super.connect(host,port);
		}
		
		public function reConnect(force:Boolean=false):void
		{
			if(this.connected)
			{
				if(force)
				{
					this.close();
					this.connect(_host,_port);
				}
			}else {
				if(this.host && this.port)
				{
					this.connect(_host,_port);
				}else{
					throw new IOError("null port or ip!");
				}
			}
		}
		protected function closeHandler(e:Event):void
		{
			if(null != onClose)
			{
				onClose();
			}
		}
		
		protected function connectHandler(e:Event):void
		{
			//trace("socket connect")
			if(null != onConnect)
			{
				onConnect();
			}
		}
		
		protected function sendNewPlayerLogin():void
		{
			
		}
		
		protected function sendCustomPlayerLogin():void
		{
			
		}
		
		protected function errorHandler(e:IOErrorEvent):void
		{
			if(null != onError)
			{
				onError(e.text);
			}
		}
		
		protected function dataHandler(e:ProgressEvent):void
		{
			if(null != onData)
			{
				onData(this);
			}
		}
		
		protected function securityError(e:SecurityErrorEvent):void
		{
			if(null != onError)
			{
				onError(e.text);
			}
		}
		
		public function set host(h:String):void
		{
			if(this.connected)
			{
				this.close();
			}
			_host = h;
			if(_host && _port)
			{
				this.connect(_host,_port);
			}
		}
		public function get host():String
		{
			return _host;
		}
		
		public function set port(p:int):void
		{
			if(this.connected)
			{
				this.close();
			}
			_port = p;
			if(_host && _port)
			{
				this.connect(_host,_port);
			}
		}
		public function get port():int
		{
			return _port;
		}
	}
}